MXMLC=/opt/flex/flex3/bin/mxmlc
HAXE=haxe

SRC_DIR=src/
EX_DIR=examples/
BIN_DIR=bin/

LICENSE=mit-license.txt

CORE=$(shell find $(SRC_DIR) -name '*.hx')

ASSET_DIR=assets/

EXAMPLES=Ex05SingleGraphic.swf                                          \
         Ex07MultipleGraphics.swf                                       \
         Ex08PartialImage.swf                                           \
         Ex10Spritesheet.swf                                            \
	 Ex11Layers.swf							\
         Ex15Keyboard.swf						\
	 Ex20SingleSound.swf						\
	 Ex23MultipleSounds.swf						\
#         Ex30ShooterPhysics.swf						\
	 Ex40Textline.swf						\
	 Ex45CustomFont.swf						\
	 Ex60BasicCollisions.swf			                \
         PreloaderDemo.swf                                              \
         DeadpoolDemo.swf

#         Ex50Deadpool.swf                                               \

EX_ASSETS=$(shell find examples/assets -type f) \
          examples/assets/yoshi.png \
          examples/assets/player.png \
          examples/assets/bullet.png

all: dirs examples

examples: $(addprefix $(BIN_DIR),$(EX_ASSETS))\
	  $(addprefix $(BIN_DIR)$(EX_DIR),$(EXAMPLES)) \

dirs:
	@if [ ! -e $(BIN_DIR) ]; then mkdir $(BIN_DIR); fi
	@if [ ! -e $(BIN_DIR)$(EX_DIR) ]; then mkdir $(BIN_DIR)$(EX_DIR); fi
	@if [ ! -e $(BIN_DIR)$(EX_DIR)$(ASSET_DIR) ]; then mkdir $(BIN_DIR)$(EX_DIR)$(ASSET_DIR); fi

#-----------------------------------------------------------------------
#-----------------------------------------------------------------------
$(BIN_DIR)examples/assets/%: examples/assets/%
	@if [ ! -e `dirname $@` ]; then mkdir -p `dirname $@`; fi
	cp $+ $@

#-- examples/assets/yoshi.png is kept in the repo so people don't need
#-- ImageMagick to build the demos.  However, this is how it's made.
$(BIN_DIR)examples/assets/yoshi.png: examples/assets/yoshi/yoshi00.png \
                                     examples/assets/yoshi/yoshi01.png \
                                     examples/assets/yoshi/yoshi02.png \
                                     examples/assets/yoshi/yoshi03.png \
                                     examples/assets/yoshi/yoshi04.png \
                                     examples/assets/yoshi/yoshi05.png
	@command -v montage >/dev/null; if [ $$? != 0 ]; then \
            echo "Montage not found; install ImageMagick to build $@"; false; fi
	montage  $+ -channel rgba -alpha on -trim -geometry "1x1<" \
                 -tile x1 -background transparent $@

#-- examples/assets/player.png
$(BIN_DIR)examples/assets/player.png: \
  examples/assets/shooter/player0.png \
  examples/assets/shooter/player1.png
	@command -v montage >/dev/null; if [ $$? != 0 ]; then \
            echo "Montage not found; install ImageMagick to build $@"; false; fi
	montage  $+ -channel rgba -alpha on -trim -geometry "1x1<" \
                 -tile x1 -background transparent $@

$(BIN_DIR)examples/assets/bullet.png: \
  examples/assets/shooter/bullet0.png \
  examples/assets/shooter/bullet1.png
	@command -v montage >/dev/null; if [ $$? != 0 ]; then \
            echo "Montage not found; install ImageMagick to build $@"; false; fi
	montage  $+ -channel rgba -alpha on -trim -geometry "1x1<" \
                 -tile x1 -background transparent $@

#-----------------------------------------------------------------------
#-----------------------------------------------------------------------
#-- This one is special because it uses an embedded graphic
$(BIN_DIR)$(EX_DIR)Ex07MultipleGraphics.swf: $(CORE) $(EX_DIR)Ex07MultipleGraphics.hx
	$(HAXE) -debug -main $(@:$(BIN_DIR)$(EX_DIR)%.swf=%) -swf $@ \
	        -swf-header 640:480:60:e0e0ff                        \
                -swf-version 10.1 -cp $(EX_DIR) -cp $(SRC_DIR)       \
                -swf-lib assets/default-font.swf                     \
                -resource examples/assets/planetoid.png@planetoid

#-----------------------------------------------
#-- Next three comprise preloader example...
$(BIN_DIR)$(EX_DIR)Game.swf: $(CORE) $(EX_DIR)preloader/Game.hx $(BIN_DIR)$(EX_DIR)game-assets.swf
	$(HAXE) -debug -main $(@:$(BIN_DIR)$(EX_DIR)%.swf=%) -swf $@ \
	        -swf-header 640:480:60:e0e0ff                        \
                -swf-version 10.1                                    \
		-cp $(EX_DIR)preloader/ -cp $(SRC_DIR)               \
                -swf-lib $(BIN_DIR)$(EX_DIR)game-assets.swf

$(BIN_DIR)$(EX_DIR)game-assets.swf: $(EX_DIR)preloader/game-assets.xml
	swfmill simple $(EX_DIR)preloader/game-assets.xml $@

$(BIN_DIR)$(EX_DIR)PreloaderDemo.swf: as3/com/rocketshipgames/as3/Preloader.as $(EX_DIR)preloader/Wrapper.as $(BIN_DIR)$(EX_DIR)Game.swf $(BIN_DIR)$(EX_DIR)game-assets.swf
	$(MXMLC) -default-frame-rate 60 \
		 -default-size 640 480 \
		 -default-background-color=#FFFFFF \
		 -use-network \
		 -optimize=true \
		 -compiler.debug \
		 -output $@ \
		 -sp as3/ \
		 -sp $(EX_DIR)preloader/ \
		 -frame=Wrapper,Wrapper \
		 -- as3/com/rocketshipgames/as3/Preloader.as
#-- End preloader example
#-----------------------------------------------

#-----------------------------------------------
#-- Next three comprise deadpool example...
$(BIN_DIR)$(EX_DIR)Ex50Deadpool.swf: $(CORE) $(EX_DIR)Ex50Deadpool.hx $(BIN_DIR)$(EX_DIR)deadpool-assets.swf
	$(HAXE) -debug -main $(@:$(BIN_DIR)$(EX_DIR)%.swf=%) -swf $@ \
	        -swf-header 640:480:60:e0e0ff                        \
                -swf-version 10.1                                    \
		-cp $(EX_DIR) -cp $(SRC_DIR)                         \
                -swf-lib $(BIN_DIR)$(EX_DIR)deadpool-assets.swf

$(BIN_DIR)$(EX_DIR)deadpool-assets.swf: $(EX_DIR)deadpool/deadpool-assets.xml
	swfmill simple $(EX_DIR)deadpool/deadpool-assets.xml $@

$(BIN_DIR)$(EX_DIR)DeadpoolDemo.swf: as3/com/rocketshipgames/as3/Preloader.as $(EX_DIR)deadpool/Wrapper.as $(BIN_DIR)$(EX_DIR)Ex50Deadpool.swf $(BIN_DIR)$(EX_DIR)deadpool-assets.swf
	$(MXMLC) -default-frame-rate 60 \
		 -default-size 640 480 \
		 -default-background-color=#FFFFFF \
		 -use-network \
		 -optimize=true \
		 -compiler.debug \
		 -output $@ \
		 -sp as3/ \
		 -sp $(EX_DIR)deadpool/ \
		 -frame=Wrapper,Wrapper \
		 -- as3/com/rocketshipgames/as3/Preloader.as
#-- End deadpool example
#-----------------------------------------------

#-----------------------------------------------
#-- Next two embed a custom font
$(BIN_DIR)$(EX_DIR)assets/origami.swf:examples/assets/origami-font.xml \
			              examples/assets/origa___.ttf
	@command -v swfmill >/dev/null; if [ $$? != 0 ]; then \
            echo "swfmill not found, must install to build $@"; false; fi
	swfmill simple examples/assets/origami-font.xml $@

$(BIN_DIR)$(EX_DIR)Ex45CustomFont.swf: $(CORE) $(EX_DIR)Ex45CustomFont.hx $(BIN_DIR)$(EX_DIR)assets/origami.swf
	$(HAXE) -debug -main $(@:$(BIN_DIR)$(EX_DIR)%.swf=%) -swf $@ \
	     -swf-header 640:480:60:e0e0ff                        \
             -swf-version 10.1 -cp $(EX_DIR) -cp $(SRC_DIR)       \
             -swf-lib assets/default-font.swf                     \
             -swf-lib $(BIN_DIR)$(EX_DIR)assets/origami.swf
#-- End custom font embed
#-----------------------------------------------

#-----------------------------------------------------------------------
#-----------------------------------------------------------------------
assets/default-font.swf:assets/default-font.xml assets/nokiafc22.ttf
	@command -v swfmill >/dev/null; if [ $$? != 0 ]; then \
            echo "swfmill not found, must install to build $@"; false; fi
	swfmill simple assets/default-font.xml $@

#-----------------------------------------------------------------------
#-----------------------------------------------------------------------
$(BIN_DIR)$(EX_DIR)%.swf: $(CORE) assets/default-font.swf $(EX_DIR)%.hx
	$(HAXE) -debug -main $(@:$(BIN_DIR)$(EX_DIR)%.swf=%) -swf $@ \
	     -swf-header 640:480:60:e0e0ff                        \
             -swf-lib assets/default-font.swf                  \
             -swf-version 10.1 -cp $(EX_DIR) -cp $(SRC_DIR)

#-----------------------------------------------------------------------
#-----------------------------------------------------------------------
clean:
	rm -rf $(BIN_DIR)
	find . -name '*~' -delete

report:
	@echo "CORE FILES"
	@echo $(CORE)
	@echo
	@echo "EXAMPLES"
	@echo $(EXAMPLES)
	@echo
	@echo "LINES FILE"
	@echo  -------------------------------------------------
	@wc -l Makefile $(CORE) $(shell find examples -name '*.hx')

attribute:
	@for i in $(CORE) $(shell find $(EX_DIR) -name '*.hx'); do head -n 2 $$i | grep Copyright > /dev/null; if [ $$? = 1 ]; then cat $(LICENSE) $$i > lic-tmp; mv lic-tmp $$i; echo Added license to $$i; fi; done

.phony: all clean report dirs attribute
